﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using points.Db;
using points.Interface;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace points.Model
{
    /*
        pk
        obj_name
        add_dt
        mod_dt
        marked_delete_y_n
        rec_id
        fk_customer_id
        fk_merchant_id
        fk_employee_id
        fk_transaction_id
        point_value
        msg
    */
    public class CustomerTransaction : DbAssist, IStorable
    {
        public int Pk { get; set; }
        public string Obj_Name { get; set; }
        public DateTime Add_Dt { get; set; }
        public DateTime Mod_Dt { get; set; }
        public string Marked_Delete_Y_N { get; set; }
        public int Rec_Id { get; set; }
        public int Fk_Customer_Id { get; set; }
        public int Fk_Merchant_Id { get; set; }
        public int Fk_Employee_Id { get; set; }
        public int Fk_Transaction_Id { get; set; }
        public int Point_Value { get; set; }
        public string Msg { get; set; }


        public CustomerTransaction
        (
            int pk,
            string obj_name,
            DateTime add_dt,
            DateTime mod_dt,
            string marked_delete_y_n,
            int rec_id,
            int fk_customer_id,
            int fk_merchant_id,
            int fk_employee_id,
            int fk_transaction_id,
            int point_value,
            string msg
        ) 
        {
            Init
            (
                pk,
                obj_name,
                add_dt,
                mod_dt,
                marked_delete_y_n,
                rec_id,
                fk_customer_id,
                fk_merchant_id,
                fk_employee_id,
                fk_transaction_id,
                point_value,
                msg
            );
        }


        protected void Init
        (
            int pk,
            string obj_name,
            DateTime add_dt,
            DateTime mod_dt,
            string marked_delete_y_n,
            int rec_id,
            int fk_customer_id,
            int fk_merchant_id,
            int fk_employee_id,
            int fk_transaction_id,
            int point_value,
            string msg
        ) 
        {
            this.Pk = pk;
            this.Obj_Name = obj_name;
            this.Add_Dt = add_dt;
            this.Mod_Dt = mod_dt;
            this.Marked_Delete_Y_N = marked_delete_y_n;
            this.Rec_Id = rec_id;
            this.Fk_Customer_Id = fk_customer_id;
            this.Fk_Merchant_Id = fk_merchant_id;
            this.Fk_Employee_Id = fk_employee_id;
            this.Fk_Transaction_Id = fk_transaction_id;
            this.Point_Value = point_value;
            this.Msg = msg;
        }

        public int Delete()
        {
            int retVal = 0;

            return retVal;
        }

        public int Update()
        {
            int retVal = 0;

            return retVal;
        }

        public int Insert()
        {
            int rowsAffected = 0;

            string strConn = (string)ConfigurationManager.ConnectionStrings["POINTS_PROD"].ToString();

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                conn.Open();

                SqlParameter paramPk = new SqlParameter("@paramPk", SqlDbType.Int);
                SqlParameter paramObj_name = new SqlParameter("@paramObj_name", SqlDbType.NVarChar);
                SqlParameter paramAdd_dt = new SqlParameter("@paramAdd_dt", SqlDbType.DateTime2);
                SqlParameter paramMod_dt = new SqlParameter("@paramMod_dt", SqlDbType.DateTime2);
                SqlParameter paramMarked_delete_y_n = new SqlParameter("@paramMarked_delete_y_n", SqlDbType.NVarChar);
                SqlParameter paramRec_id = new SqlParameter("@paramRec_id", SqlDbType.Int);
                SqlParameter paramFk_customer_id = new SqlParameter("@paramFk_customer_id", SqlDbType.Int);
                SqlParameter paramFk_merchant_id = new SqlParameter("@paramFk_merchant_id", SqlDbType.Int);
                SqlParameter paramFk_employee_id = new SqlParameter("@paramFk_employee_id", SqlDbType.Int);
                SqlParameter paramFk_transaction_id = new SqlParameter("@paramFk_transaction_id", SqlDbType.Int);
                SqlParameter paramPoint_value = new SqlParameter("@paramPoint_value", SqlDbType.Int);
                SqlParameter paramMsg = new SqlParameter("@paramMsg", SqlDbType.NVarChar);

                paramPk.Value = this.Pk;
                paramObj_name.Value = this.Obj_Name;
                paramAdd_dt.Value = this.Add_Dt;
                paramMod_dt.Value = this.Mod_Dt;
                paramMarked_delete_y_n.Value = this.Marked_Delete_Y_N;
                paramRec_id.Value = this.Rec_Id;
                paramFk_customer_id.Value = this.Fk_Customer_Id;
                paramFk_merchant_id.Value = this.Fk_Merchant_Id;
                paramFk_employee_id.Value = this.Fk_Employee_Id;
                paramFk_transaction_id.Value = this.Fk_Transaction_Id;
                paramPoint_value.Value = this.Point_Value;
                paramMsg.Value = this.Msg;

                using (SqlCommand cmd = conn.CreateCommand())
                {
                    string sqlString =
                    " INSERT INTO customer_transaction" +
                        " (" +
                        //"     pk," +
                        "     obj_name," +
                        "     add_dt," +
                        "     mod_dt," +
                        "     marked_delete_y_n," +
                        "     rec_id," +
                        "     fk_customer_id," +
                        "     fk_merchant_id," +
                        "     fk_employee_id," +
                        "     fk_transaction_id," +
                        "     point_value," +
                        "     msg" +
                        " )" +
                        " VALUES" +
                        " (" +
                        //"     @paramPk," +
                        "     @paramObj_name," +
                        "     @paramAdd_dt," +
                        "     @paramMod_dt," +
                        "     @paramMarked_delete_y_n," +
                        "     @paramRec_id," +
                        "     @paramFk_customer_id," +
                        "     @paramFk_merchant_id," +
                        "     @paramFk_employee_id," +
                        "     @paramFk_transaction_id," +
                        "     @paramPoint_value," +
                        "     @paramMsg" +
                        " )"
                    ;

                    cmd.CommandText = sqlString;

                    cmd.Parameters.Add(paramPk);
                    cmd.Parameters.Add(paramObj_name);
                    cmd.Parameters.Add(paramAdd_dt);
                    cmd.Parameters.Add(paramMod_dt);
                    cmd.Parameters.Add(paramMarked_delete_y_n);
                    cmd.Parameters.Add(paramRec_id);
                    cmd.Parameters.Add(paramFk_customer_id);
                    cmd.Parameters.Add(paramFk_merchant_id);
                    cmd.Parameters.Add(paramFk_employee_id);
                    cmd.Parameters.Add(paramFk_transaction_id);
                    cmd.Parameters.Add(paramPoint_value);
                    cmd.Parameters.Add(paramMsg);

                    SqlTransaction transaction = null;
                    try
                    {
                        //conn.Open();
                        transaction = conn.BeginTransaction();
                        cmd.Transaction = transaction;
                        rowsAffected = cmd.ExecuteNonQuery();
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                    finally
                    {
                        try
                        {
                            conn.Close();
                        }
                        catch { }
                    }
                }
            }
            return rowsAffected;
        }
    }
}